#!/usr/bin/env python2.4
__license__ = "GNU Lesser General Public License, Ver.2"
__author__ = "Grigory Bakunov <bobuk@justos.org>"

import web
import markdown
import os
import wmtools

from wmauth import *
from config import *
from wmconfig import Config

class deltext:
    def GET(self, pagename):
        (user, edit) = getAccess(web.cookies())
        if os.path.isfile(pagesPath + pagename + '.mdn') and edit:
            for ext in ['html','mdn']:
                os.remove(pagesPath + pagename + '.' + ext)
        web.redirect(Config().cfg['c_abspath'] + '/txt.' + pagename)

class text:
    def GET(self, pagename):
        (user, edit) = getAccess(web.cookies())
        if os.path.isfile(pagesPath + pagename + '.mdn'):
            page = open(pagesPath + pagename +'.html').read()
            web.render('text.html')
        else:
            if edit:
                web.render('text.new.html')
            else:
                web.notfound()
                                
    def POST(self, pagename):
        (user, edit) = getAccess(web.cookies())
        if not edit:
            wmtools.raiseError(2)
        inp = web.input()
        _fl = open(pagesPath + pagename + '.mdn','w'); _fl.write(inp.etext); _fl.close()
        _fl = open(pagesPath + pagename + '.html','w'); _fl.write(markdown.markdown(inp.etext)); _fl.close()
        web.redirect(Config().cfg['c_abspath'] + '/txt.' + pagename)
